package com.ldk.admin.factory;

import java.util.ArrayList;
import java.util.List;

import com.ldk.framework.entity.Area;

public class AreaFactory {
    /**
     * 菜单树建立器
     * @param areas
     * @return
     */
    public List<AreaExt> build(List<Area> areas)
    {
        if(areas == null) return null;
        List<AreaExt> regionsExt = new ArrayList<AreaExt>();
        for(Area area: areas){
            //根菜单
            if(area.getParentId() == 0){
                AreaExt areaExt = new AreaExt();
                areaExt.setAreaId(area.getAreaId());
                areaExt.setParentId(area.getParentId());
                areaExt.setAreaName(area.getAreaName());
                areaExt.setAreaLogo(area.getAreaLogo());
                areaExt.setSortOrder(area.getSortOrder());
                areaExt.setAreaCode(area.getAreaCode());

                List<AreaExt> children = buildChild(area, areas);
                areaExt.setChildren(children);

                regionsExt.add(areaExt);
            }
        }
        return regionsExt;
    }

    private List<AreaExt> buildChild(Area parent, List<Area> areas){
        if(parent == null || areas == null) return null;
        List<AreaExt> areasExt = new ArrayList<AreaExt>();
        for(Area area:areas){
            if(area.getParentId() == parent.getAreaId()){
                AreaExt areaExt = new AreaExt();
                areaExt.setAreaId(area.getAreaId());
                areaExt.setParentId(area.getParentId());
                areaExt.setAreaName(area.getAreaName());
                areaExt.setAreaLogo(area.getAreaLogo());
                areaExt.setSortOrder(area.getSortOrder());
                areaExt.setAreaCode(area.getAreaCode());

                List<AreaExt> children = buildChild(area, areas);
                areaExt.setChildren(children);
                areasExt.add(areaExt);

                if(children == null) continue;
            }
        }
        return areasExt;
    }
}
